package com.lanou.beautifulwardrobe.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.lanou.beautifulwardrobe.dbHelper.UsersDBHelper;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.contants.ToastCommom;
import com.lanou.beautifulwardrobe.entity.greendao.UsersEntity;

/**
 * Created by dllo on 15/11/3.
 */
public class LoginActivity extends Activity implements View.OnClickListener {
    private Button loginBtn, loginReister;
    private TextView loginForgetPwd;
    private EditText loginUser, loginPwd;
    private ImageButton loginBack;
    private UsersDBHelper helper = UsersDBHelper.getInstance();
    private TextView qqLogin, sianLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    private void initView() {
        loginBtn = (Button) findViewById(R.id.loginBtn);
        loginReister = (Button) findViewById(R.id.loginRegister);
        loginForgetPwd = (TextView) findViewById(R.id.loginForgetPwd);
        loginUser = (EditText) findViewById(R.id.loginUser);
        loginPwd = (EditText) findViewById(R.id.loginPwd);
        loginBack = (ImageButton) findViewById(R.id.loginBack);
        qqLogin = (TextView) findViewById(R.id.qqLogin);
        sianLogin = (TextView) findViewById(R.id.sianLogin);
        listener();
    }

    private void listener() {
        loginBtn.setOnClickListener(this);
        loginReister.setOnClickListener(this);
        loginForgetPwd.setOnClickListener(this);
        loginBack.setOnClickListener(this);
        qqLogin.setOnClickListener(this);
        sianLogin.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.loginBtn:
                for (UsersEntity u : helper.queryAll()) {
                    Log.i("lanou", u.getUser());
                }

                if (!(helper.isSaved(loginUser.getText().toString().trim()))) {
                    ToastCommom.makeText(this, "用户名不存在,请重新输入", Toast.LENGTH_SHORT).show();
                } else {
                    Intent intent = new Intent();
                    intent.putExtra("userName", loginUser.getText().toString().trim());

                    intent.putExtra("userHead", helper.queryByUser(loginUser.getText().toString().trim()).getHead());
                    setResult(300, intent);
                    ToastCommom.makeText(this, "用户" + loginUser.getText().toString() + "登录成功", Toast.LENGTH_SHORT).show();


                    finish();
                }

                break;
            case R.id.loginRegister:
                Intent registerIntent = new Intent(this, RegisterActivity.class);
                startActivity(registerIntent);
                break;
            case R.id.loginForgetPwd:
                Intent forgetIntent = new Intent(this, RequsetActivity.class);
                startActivity(forgetIntent);
                break;
            case R.id.loginBack:
                finish();
                break;
            case R.id.qqLogin:
                Intent intent = new Intent();
                intent.putExtra("qq", true);
                setResult(300, intent);
                finish();

                break;
            case R.id.sianLogin:
                ToastCommom.makeText(this, "我还有事,我先走了哈", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
